/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Extractor.

    Maxsi Extractor is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Extractor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Extractor.  If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiExtractorContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiExtractorContributors.h. For more information please see
	MaxsiExtractorContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]


	MaxsiExtractor.exe
	A self-extracting application able to store a series of files inside it
	and able to extract these files to the current working directory, run the
	files, and once done, delete all the files.

	Main.cpp
	Basic WIN32 functions and stuff

****************************************************************************/

#include "MaxsiEngine.h"
#include <io.h>
#include <direct.h>
#include <iostream> 
#include <fcntl.h>
#include <SYS\Stat.h>
#include "Main.h" 
#include "UI.h" 
#include "Data.h" 

ME_WIN32ENTRYPOINT(MaxsiExtractor);

/****************************************************************************

    FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
    PURPOSE: calls initialization function, processes message loop

****************************************************************************/

int WINAPI MaxsiExtractor::WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
	return BaseClass::WinMain(hInstance,hPrevInstance,lpCmdLine,nCmdShow);
}


/****************************************************************************

    FUNCTION: InitApplication(HANDLE)
    PURPOSE: Initializes window data and registers window class

****************************************************************************/

BOOL MaxsiExtractor::InitApplication(
    HANDLE hInstance)
{
    WNDCLASS  wc = {0};

    wc.lpfnWndProc = (WNDPROC) MaxsiExtractorProc;
    wc.hInstance = (HINSTANCE)hInstance;
    wc.hIcon = LoadIconA((HINSTANCE)hInstance, "ProductIcon");
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); 
	wc.lpszMenuName = TEXT("MainWindowMenu");
    wc.lpszClassName = TEXT("MaxsiExtractor");

	if (!RegisterClass(&wc)) { return FALSE; }

	return TRUE;
}


/****************************************************************************

    FUNCTION:  InitInstance(HANDLE, int)
    PURPOSE:  Saves instance handle and creates main window

****************************************************************************/
BOOL MaxsiExtractor::InitInstance(
    HINSTANCE hInstance, 
    INT nCmdShow)
{
    LPTSTR lpszText = NULL;
    BOOL bSuccess = TRUE;

    hInst = hInstance;

    hAccTable = LoadAccelerators(hInst, TEXT("MaxsiExtractor"));

	// Get the screen size
	DEVMODE Dev;
	EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&Dev);

	int w = 500;
	int h = 370;
	int x = (Dev.dmPelsWidth - w ) / 2;
	int y = (Dev.dmPelsHeight - h ) / 2;

	char*	WindowTitle		=	"Maxsi Self-Extractor";

	if ( WindowHeader ) { WindowTitle = WindowHeader; }

    WindowHWND = CreateWindowA(
        "MaxsiExtractor",
        WindowTitle,
        (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX ),
        x, y,
        w, h,
        NULL, NULL, hInstance, NULL );

	if (!WindowHWND) { return FALSE; }


	ME_Window*	Parent	=	ME_WindowGetPointer(WindowHWND);

	Form = InitializeForm(new PageExtractor, Parent);

	ShowWindow(WindowHWND, nCmdShow);
    bSuccess = UpdateWindow(WindowHWND);

	return bSuccess;
}

/****************************************************************************

    FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
    PURPOSE:  Processes messages

****************************************************************************/

LRESULT APIENTRY MaxsiExtractor::WindowProc(
    HWND hWnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
	return BaseClass::WindowProc(hWnd,message,wParam,lParam);
}


DWORD MaxsiExtractor::OnThreadRegistered(HWND hWnd)
{
	PostMessage(hWnd,WM_COMMAND,0,0);

	return BaseClass::OnThreadRegistered(hWnd);
}